package impls.agents.dialogs;

import impls.classes.Launcher;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JDialog;

public class CarAgentDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = -4236576464840984515L;
	
	private Launcher owner;

	private TextField fuel;

	private Checkbox autoGenerate;

	private ArrayList<Integer> fuelValues;

	private int carNumbers;

	public CarAgentDialog(Launcher owner, int carNumbers) {
		super(owner);
		this.owner = owner;
		this.carNumbers = carNumbers;
		this.setModal(true);
		this.setResizable(false);
		this.setLayout(new FlowLayout());
		this.setTitle("Car Agent 1");

		fuel = new TextField("120");

		this.getContentPane().add(new Label("Fuel:"));
		this.getContentPane().add(fuel);

		Button next = new Button("Next");
		next.addActionListener(this);

		this.getContentPane().add(next);

		Button cancel = new Button("Cancel");
		cancel.addActionListener(this);

		this.getContentPane().add(cancel);

		autoGenerate = new Checkbox("Use same settings for all Agents");
		autoGenerate.setState(true);

		this.getContentPane().add(autoGenerate);

		fuelValues = new ArrayList<Integer>();

		pack();
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Cancel")) {
			owner.setAbort();
			this.dispose();
		}

		if (e.getActionCommand().equals("Next")) {
			if (autoGenerate.getState()) {
				owner.createCarsAutomatic(Integer.valueOf(fuel.getText()));
				this.dispose();
			} else {
				fuelValues.add(Integer.valueOf(fuel.getText()));
				if (fuelValues.size() + 1 <= carNumbers) {
					this.setTitle("Car Agent " + (fuelValues.size() + 1));
				} else {
					owner.createCars(fuelValues);
					this.dispose();
				}
			}
		}
	}
}
